<template>
  <div class="page-container">
    <div class="main-layout">
      <el-container class="full-height">
        <!-- 左侧侧边栏 -->
        <el-aside width="320px" class="aside-box">
          <el-container class="full-height" direction="vertical">
            <!-- 搜索栏 -->
            <el-header height="60px" class="no-padding header">
              <div class="from">
                <el-input
                  v-model="input"
                  :prefix-icon="Search"
                  placeholder="搜索好友 / 群组"
                  size="default"
                />
              </div>

              <!-- 工具栏 -->
              <div class="tools" v-outside="closeSubMenu">
                <el-button
                  :icon="Plus"
                  circle
                  plain
                  size="default"
                  @click="submenu = !submenu"
                />

                <transition name="el-zoom-in-top">
                  <div class="tools-menu" v-show="submenu">
                    <div class="menu1-item" @click="triggerSubMenu(1)">
                      创建群组
                    </div>
                    <div class="menu1-item" @click="triggerSubMenu(2)">
                      添加好友
                    </div>
                  </div>
                </transition>
              </div>
            </el-header>

            <!-- 对话列表栏 -->
            <el-main class="no-padding no-select">
              <router-link
                v-for="(menu, index) in menus"
                :to="menu.path"
                :key="index"
              >
                <div class="menu-list" :class="{ selectd: currentRoute == menu.path }" v-text="menu.name" />
              </router-link>
            </el-main>
          </el-container>
        </el-aside>

        <!-- 聊天面板容器 -->
        <el-main class="no-padding ov-hidden">
          <router-view />
        </el-main>
      </el-container>
    </div>
    <!-- 创建群聊组件 -->
    <GroupLaunch
      v-if="launchGroupShow"
      @close="launchGroupShow = false"
      @create-success="launchSuccess"
    />

    <!-- 用户查询 -->
    <UserSearch ref="searchUsers" />
  </div>
</template>

<script>
import { computed, reactive, ref, toRefs } from 'vue'
import { useRoute } from 'vue-router'
import { Search, Plus } from '@element-plus/icons-vue'
import GroupLaunch from '/@/views/chat/components/group/GroupLaunch.vue'
import UserSearch from '/@/views/chat/components/user/UserSearch.vue'
import { toTalk } from '/@/views/chat/utils/talk.js'

export default {
    name: 'phoneBook',
    components: {
      GroupLaunch,
      UserSearch
    },
    setup() {
      const route = useRoute()
      const searchUsers = ref()
      const state = reactive({
        launchGroupShow: false,
        // 查询关键词
        input: '',
        submenu: false,
        menus: [
          {
            name: '新的联系人',
            path: '/chat/phonebook/apply',
          },
          {
            name: '我的好友',
            path: '/chat/phonebook/friends',
          },
          {
            name: '我的群组',
            path: '/chat/phonebook/groups',
          },
        ]
      })
      const currentRoute = computed(() => {
        return route.path
      })
      // header 功能栏隐藏事件
      const closeSubMenu = () => {
        state.submenu = false
      }
      // 工具栏事件
      const triggerSubMenu = (type) => {
        closeSubMenu()
        if (type == 1) {
          state.launchGroupShow = true
        } else {
          searchUsers.value.open()
        }
      }
      // 发起群聊回调事件
      const launchSuccess = (data) => {
        toTalk(2, data.group_id)
      }
      return {
        ...toRefs(state),
        Search,
        Plus,
        currentRoute,
        searchUsers,
        closeSubMenu,
        triggerSubMenu,
        launchSuccess
      }
    }
}
</script>

<style lang="scss" scoped>
a{
  -webkit-touch-callout: none;
  text-decoration: none;
}
:deep(.el-button i.el-icon){
  margin-right: 0;
}
:deep(.el-main) {
  background: var(--next-color-white);
}
.page-container{
  padding: 0 !important;
  height: 100vh;
}
.main-layout {
  height: 100%;
}
.menu-list {
  height: 42px;
  display: flex;
  align-items: center;
  padding-left: 20px;
  cursor: pointer;
  border-left: 3px solid white;
  position: relative;
  transition: 0.2s ease-in;
  font-size: 14px;
  color: black;

  &:hover {
    background-color: var(--next-bg-main-color);
    border-color: #eff0f1;
  }

  &.selectd {
    border-color: var(--el-color-primary) !important;
    background-color: #eff0f1;
  }
}
.aside-box {
  position: relative;
  border-right: 1px solid var(--el-border-color-light);
  overflow: hidden;
  padding: 0;
  transition: width 0.3s;
  background: var(--next-color-white);
  .header {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 0 15px;

    .from {
      flex: 1 1;
      flex-shrink: 0;

      :deep(.el-input .el-input__inner) {
        border-radius: 20px;
        width: 175px;
      }
    }

    .tools {
      flex-basis: 32px;
      flex-shrink: 0;
      height: 32px;
      cursor: pointer;
      text-align: center;
      position: relative;
      user-select: none;
      margin-left: 15px;

      .tools-menu {
        position: absolute;
        right: 0;
        top: 38px;
        width: 100px;
        min-height: 80px;
        box-sizing: border-box;
        background-color: rgba(31, 35, 41, 0.9);
        border-radius: 5px;
        z-index: 1;
        padding: 3px 0;

        .menu1-item {
          height: 40px;
          line-height: 40px;
          color: white;
          font-size: 14px;

          &:hover {
            background-color: rgba(70, 72, 73, 0.9);
          }
        }
      }
    }
  }
}

// 右侧面板
.panel {
  position: relative;
  width: 100%;
  height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    &.border{
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .subheader {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    border-top: 1px solid var(--el-border-color-light);
    border-bottom: 1px solid var(--el-border-color-light);

    p {
      padding: 0 10px;
      cursor: pointer;
      font-size: 13px;

      &:first-child {
        padding-left: 0;
      }

      &.active {
        color: var(--el-color-primary);
      }
    }
  }

  .panel-body {
    overflow: auto;
    width: 100%;
    height: 100%;
    box-sizing: border-box;

    .preloading {
      height: 100%;
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      user-select: none;

      p {
        margin-top: 20px;
        color: #afacac;
        font-size: 14px;
        font-weight: 300;
      }
    }

    .data-item {
      display: flex;
      flex-direction: row;
      align-items: center;
      height: 60px;
      cursor: pointer;
      padding: 5px 15px;
      position: relative;
      overflow: hidden;
      border-bottom: 1px solid #f1ebeb;
      margin-bottom: 2px;

      .avatar {
        height: 35px;
        width: 35px;
        flex-basis: 35px;
        flex-shrink: 0;
        background-color: var(--el-color-primary);
        border-radius: 50%;
        overflow: hidden;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 14px;
        color: white;
        user-select: none;
        transition: ease 1s;
        position: relative;
      }

      .card {
        height: 40px;
        display: flex;
        align-content: center;
        flex-direction: column;
        flex: 1 1;
        margin-left: 10px;
        overflow: hidden;

        .title {
          width: 100%;
          height: 20px;
          display: flex;
          align-items: center;

          .name {
            margin-right: 15px;
            color: #1f2329;
          }

          .larkc-tag {
            font-size: 12px;
            font-weight: 400;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 0 6px;
            height: 20px;
            border-radius: 2px;
            cursor: default;
            user-select: none;
            background-color: #dee0e3;
            transform: scale(0.8);
            transform-origin: left;
            flex-shrink: 0;
          }

          .wait {
            background: #ffb445;
            color: white;
          }

          .agree {
            background: #53bd53;
            color: white;
          }
        }

        .content {
          font-size: 10px;
          line-height: 18px;
          color: #8f959e;
          overflow: hidden;
          margin-top: 3px;
          font-weight: 300;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }

      .apply-from {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        position: relative;
        right: -110px;
        top: 0px;
        height: 60px;
        width: 100px;
        transition: ease 0.5s 0.3s;
        background-color: white;
        opacity: 0;
        button {
          margin: 2px;
        }
      }

      &:hover {
        box-shadow: 0 0 8px 4px #f1f1f1;

        .avatar {
          border-radius: 2px;
        }

        .apply-from {
          opacity: 1;
          right: 0px;
        }
      }
    }
  }
}

.broadside-box {
  position: absolute;
  width: 350px;
  height: 100%;
  top: 0;
  right: 0;
  z-index: 2;
  animation: showBox 0.5s ease-in-out;
  -webkit-animation: showBox 0.5s ease-in-out;
  -moz-animation: showBox 0.5s ease-in-out;
  -webkit-box-direction: normal;
  background: white;
  box-shadow: 0 0 14px #cccccc70;
}

@keyframes showBox {
  0% {
    transform: translateX(350px);
  }

  to {
    transform: translateX(0);
  }
}

@-webkit-keyframes showBox {
  0% {
    -webkit-transform: translateX(350px);
  }

  to {
    -webkit-transform: translateX(0);
  }
}
</style>